/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package vescapecloud.openstack;

import java.util.ArrayList;
import org.openstack4j.api.Builders;
import org.openstack4j.api.OSClient;
import org.openstack4j.api.exceptions.AuthenticationException;
import org.openstack4j.model.compute.ActionResponse;
import org.openstack4j.model.compute.FloatingIP;
import org.openstack4j.model.compute.Server;
import org.openstack4j.model.compute.ServerCreate;
import org.openstack4j.openstack.OSFactory;

/**
 * Instance class : represent a new instance created for an user Contain
 * parameter of the user connected and the link with OpenStack API used to
 * anthentification and boot.
 *
 * @author Cloye & Dumons
 */
public class Instance {

    /* Configuration  for OpenStack Instance */
    // ID of Snapshot used to run an instance
    private final String GREEDY_IMAGE_ID = "840f1631-0cb9-4711-b618-9140e3df6114";

    // ID of the flavor used for the new instance
    private final String GREEDY_FLAVOR_ID = "4b42c24b-bfd4-4e24-9517-36cc433e6397";

    // ID of the network installed on the new instance
    private final String NETWORK_ID = "39366143-32ec-4dc2-a26c-fc11dcbd2592";

    // Prefix of the future instance 
    private final String GREEDY_INSTANCE_PREFIX = "GreedyRabbit";

    // Name of external network for the instance
    private final String EXT_NETWORK = "ext-net";

    // Authentification client
    private OSClient os;

    /* User parameters */
    private String user_ip;
    private String account_type;
    private String user_id;
    private int user_API_level;
    private int user_resolution_width;
    private int user_resolution_height;
    private String user_gameselected;
    private int status;

    /**
     * Constructor without parameter
     */
    public Instance() {

        this.user_ip = null;
        this.account_type = null;
        this.user_id = null;
        this.user_API_level = 0;
        this.user_resolution_height = 0;
        this.user_resolution_width = 0;
        this.user_gameselected = null;
        this.status = 0;

    }

 

    /**
     * Method to connect to OpenStack cloud with vescape account.
     *
     * @throws AuthenticationException
     */
    public void authentification()
            throws AuthenticationException {

        os = OSFactory.builder()
                .endpoint("http://10.0.0.11:5000/v2.0")
                .credentials("vescape", "Vescape")
                .tenantName("vescape")
                .authenticate();

        System.out.println("[Instance       ]     Authentification to OpenStack ok");
    }

    /**
     * Boot a new instance
     */
    public void bootServer() throws InterruptedException {

        // network list
        ArrayList<String> netList = new ArrayList<String>();
        netList.add(NETWORK_ID);

        // Create a Server Model Object
        ServerCreate sc = Builders
                .server()
                .name(GREEDY_INSTANCE_PREFIX + "_" + user_ip)
                .flavor(GREEDY_FLAVOR_ID)
                .image(GREEDY_IMAGE_ID)
                .networks(netList)
                .addPersonality("/home/test.txt", "coucou")
                .build();
        System.out.println("[Instance       ]     Server Model Object created");

        // Boot the server
        Server server = os.compute()
                .servers()
                .boot(sc);
        System.out.println("[Instance       ]     Server boot");

//        os.compute().servers().action(server.getId(), Action.START);
        Thread.sleep(3000);
        //associate ip address of server to a floating ip adress
        os = OSFactory.builder()
                .endpoint("http://10.0.0.11:5000/v2.0")
                .credentials("vescape", "Vescape")
                .tenantName("vescape")
                .authenticate();
        FloatingIP ip = os.compute().floatingIps().allocateIP(EXT_NETWORK);

        ActionResponse r = os
                .compute()
                .floatingIps()
                .addFloatingIP(
                        server, server.getAccessIPv4(), "10.0.2.53");

        System.out.println("[Instance       ]     Floating IP address associated");
    }

    public String getUser_ip() {
        return user_ip;
    }

    public String getAccount_type() {
        return account_type;
    }

    public String getUser_id() {
        return user_id;
    }

    public int getUser_API_level() {
        return user_API_level;
    }

    public int getUser_resolution_width() {
        return user_resolution_width;
    }

    public int getUser_resolution_height() {
        return user_resolution_height;
    }

    public String getUser_gameselected() {
        return user_gameselected;
    }

    public int getStatus() {
        return status;
    }

    public void setUser_ip(String user_ip) {
        this.user_ip = user_ip;
    }

    public void setAccount_type(String account_type) {
        this.account_type = account_type;
    }

    public void setUser_id(String user_id) {
        this.user_id = user_id;
    }

    public void setUser_API_level(int user_API_level) {
        this.user_API_level = user_API_level;
    }

    public void setUser_resolution_width(int user_resolution_width) {
        this.user_resolution_width = user_resolution_width;
    }

    public void setUser_resolution_height(int user_resolution_height) {
        this.user_resolution_height = user_resolution_height;
    }

    public void setUser_gameselected(String user_gameselected) {
        this.user_gameselected = user_gameselected;
    }

    public void setStatus(int status) {
        this.status = status;
    }

}
